package com.joinlabs.action;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.context.annotation.SessionScope;
import org.springframework.web.servlet.ModelAndView;

import com.joinlabs.entity.Assess;
import com.joinlabs.entity.AssessOrder;
import com.joinlabs.entity.Enshrine;
import com.joinlabs.entity.Img;
import com.joinlabs.entity.Orderitem;
import com.joinlabs.entity.PageBean;
import com.joinlabs.entity.ProTypeBean;
import com.joinlabs.entity.Wares;
import com.joinlabs.entity.conditionBean;
import com.joinlabs.service.IImgService;
import com.joinlabs.service.IProductService;

@Controller
@RequestMapping("/product")
@SessionAttributes(value = {"pageBean","usercolor"})
public class WaresAction {

	@Resource
	private IProductService waresService;

	@Resource
	private IImgService imgService;

	
	//商品详情显示
	@RequestMapping("/pro")
	public ModelAndView showwares(int pid, ModelMap map) {

		Wares wares = waresService.selectByPrimaryKey(pid);

		int count = waresService.selectByprocount(new BigDecimal(pid));
		
		map.addAttribute("wares", wares);
		map.addAttribute("count", count);

		
		return new ModelAndView("/product/prodetail");
	}

	//商品参数显示
	@RequestMapping("/proconfig")
	public ModelAndView showconfig(int waresId,int count, ModelMap map) {

		Img imglist = imgService.selectByPrimaryKey(waresId);

		Wares wares = waresService.selectByPrimaryKey(waresId);

		String waresName = wares.getWaresName();

		map.addAttribute("imglist", imglist);
		map.addAttribute("waresName", waresName);
		map.addAttribute("waresId", waresId);
		map.addAttribute("count", count);
		
		return new ModelAndView("/product/config");
	}
	
	//商品概述显示
	@RequestMapping("/proskr")
	public ModelAndView showskr(int waresId,int count,ModelMap map) {

		Img imglist = imgService.selectByPrimaryKey(waresId);

		Wares wares = waresService.selectByPrimaryKey(waresId);

		String waresName = wares.getWaresName();

		map.addAttribute("imglist", imglist);
		map.addAttribute("waresName", waresName);
		map.addAttribute("waresId", waresId);
		map.addAttribute("count", count);
		
		return new ModelAndView("/product/skr");
	}

	//商品类别显示
	@RequestMapping("/showtype")
	public ModelAndView showtype(int tid, ModelMap map) {

		List<Wares> wareslist = waresService.selectBytypeId(tid);

		String typeName = waresService.selectBytypeName(tid);

		map.addAttribute("wareslist", wareslist);
		map.addAttribute("typeName", typeName);

		return new ModelAndView("/product/showtype");
	}

	//商品搜索显示
	@RequestMapping("/selectlike")
	public ModelAndView selectlike(String likeName, ModelMap map) {

		List<Wares> wareslist = waresService.selectBylikeName(likeName);

		map.addAttribute("wareslist", wareslist);

		map.addAttribute("typeName", likeName);

		if (wareslist.size() == 0) {

			String nonull = "kia";
			map.addAttribute("nonull", nonull);
		}

		return new ModelAndView("/product/showtype");
	}

	//分页类别显示
	@RequestMapping("/fytype")
	public ModelAndView showfytype(ModelMap map) {

		List<ProTypeBean> typelist = waresService.selectfyTypeName();

		int allprocount = 0;

		for (int i = 0; i < typelist.size(); i++) {
			BigDecimal j = typelist.get(i).getTcount();
			allprocount = allprocount + j.intValue();
		}

		map.addAttribute("typelist", typelist);
		map.addAttribute("allprocount", allprocount);

		return new ModelAndView("/product/classify");
	}

	//商品多条件显示
	@RequestMapping("/showfy")
	public ModelAndView showfy(conditionBean condition, ModelMap map) {

		
		//查询出符合条件的商品集合
		List<Wares> wareslist = waresService.selectBytypewaresName(condition);
		System.out.println("传进来的值："+condition);
		//查询分页实体
		PageBean pageBean = new PageBean();
		pageBean.setCurPage(condition.getCurPage());
		
		//一页显示的数量
		pageBean.setPageCount(12);
		//开始到结束的下标
		pageBean.calcIndex();
		//总共有多少页
		pageBean.calcPages(wareslist.size());
		
		map.addAttribute("wareslist", wareslist);

		map.addAttribute("pageBean", pageBean);
		
		System.out.println(pageBean);
		
		if (wareslist.size() == 0) {

			String nonull = "kia";
			map.addAttribute("nonull", nonull);
		}

		return new ModelAndView("/product/seldetail");
	}

		//商品评价显示
		@RequestMapping("/prolevel")
		public ModelAndView showlevel(int waresId,int count,ModelMap map) {

			List<Assess> asslist = waresService.selectBywaresid(new BigDecimal(waresId));

			List<Orderitem> ordlist = waresService.selectwaresidord(new BigDecimal(waresId));

			Wares wares = waresService.selectByPrimaryKey(waresId);
			//Wares wares = waresService.selectByPrimaryKey(waresId);
			
			List<AssessOrder> assord = new ArrayList<AssessOrder>();
			
			for(int i=0;i < asslist.size();i++) {
				
				AssessOrder asor = new AssessOrder();
				asor.setAssessId(asslist.get(i).getAssessId());
				asor.setAssessLevel(asslist.get(i).getAssessLevel());
				asor.setAssessSelltext(asslist.get(i).getAssessSelltext());
				asor.setAssessUsertext(asslist.get(i).getAssessUsertext());
				asor.setWaresId(asslist.get(i).getWaresId());
				asor.setUserId(asslist.get(i).getUserId());
				asor.setOrderItemid(asslist.get(i).getOrderItemid());
				
				asor.setWaresName(wares.getWaresName());
				asor.setOrderPrice(ordlist.get(i).getOrderPrice());
				asor.setWaresColor(ordlist.get(i).getWaresColor());
				asor.setWaresEdition(ordlist.get(i).getWaresEdition());
				asor.setImgpath(ordlist.get(i).getImgpath());
				
				System.out.println(asor);
				assord.add(asor);
			}
			
			
			map.addAttribute("assord", assord);
			map.addAttribute("waresId", waresId);
			map.addAttribute("waresName",wares.getWaresName());
			map.addAttribute("count", count);
			//map.addAttribute("wares", wares);

			return new ModelAndView("/product/level");
		}
		
		
		//跳转地图定位
		@RequestMapping("/addtest")
		public ModelAndView addtest( ModelMap map) {

		
			return new ModelAndView("/product/addtest");
		}
		
		//颜色选择器
		@RequestMapping("/procolor")
		@ResponseBody
		public String procolor(String usercolor,ModelMap map) {

			//System.out.println("The color is-----------------"+usercolor);
			
			map.addAttribute("usercolor", usercolor);
			
			return "success";
		}
		
		
		//收藏选择器
		@RequestMapping("/enshrine")
		@ResponseBody
		public String enshrine(Enshrine enshrine,ModelMap map) {

			System.out.println(enshrine);
			
			 List<Enshrine> i = waresService.selectuserwares(enshrine);
			
			 System.out.println(i);
			 
			if(i.size() < 1) {
				
				int row = waresService.insertenshrine(enshrine);
				
				return "收藏成功，收藏+"+row;
			}
			
			return "收藏失败,您已添加过该商品";
		}		
		
		//查看评价
		@RequestMapping("/addlevel")
		public ModelAndView orderlevel(String waresName,int orderItemid,ModelMap map) {

//			String waresName = "testpro";
//			int item = 7;

			Orderitem orderitem = waresService.selectByorderitemid(orderItemid);
			
			BigDecimal pid = orderitem.getWaresId();
			orderitem.setWaresName(waresName);
			
			map.addAttribute("orderitem", orderitem);

			int count = waresService.selectByprocount(pid);
			
			map.addAttribute("count", count);
			
			Assess ass = waresService.selectByorderItemid(orderItemid);		
			
			if(ass != null) {
				
				int exist = 1;
				
				map.addAttribute("exist", exist);
				map.addAttribute("ass", ass);
				
				return new ModelAndView("/product/addlevel");
			}
			
			int exist = 0;
			
			map.addAttribute("exist", exist);
			
			return new ModelAndView("/product/addlevel");
		}		
		
		//提交评价
		@RequestMapping("/commitlevel")
		public ModelAndView commitlevel(Assess assess,ModelMap map) {

			BigDecimal pid = assess.getWaresId();
			
			int row = waresService.insertassess(assess);

			return new ModelAndView("redirect:/product/pro?pid="+pid+"");
		}		
		
}
